<?php
/**
 * User: 五哥派
 * Date: 2024/6/25
 * Tel(同WX): 17335592719
 */
namespace app\admin\controller;

use app\admin\model\SystemAdmin as AdminModel;
use app\admin\validate\SystemAdmin as AdminValidate;
use think\Controller;

class Login extends Controller{

    public function index(){
        ob_clean();
        return $this->fetch();
    }

    public function check(){
        if(!request()->isPost()) {
            return error('请求方式错误');
        }
        $account = $this->request->param("account", "", "trim");
        $password = $this->request->param("password", "", "trim");
        //$captcha = $this->request->param("captcha", "", "trim");
        $data = [
            "account" => $account,
            "password" => $password,
            //"captcha" => $captcha,
        ];
        $adminValidate = new AdminValidate();
        if(!$adminValidate->check($data)){
            //return error($adminValidate->getError());
        }

        $adminModel = new AdminModel();
        $adminData = $adminModel->findData(['account' => trim($account)]);
        if(empty($adminData) || ($adminData->status != 1)){
            return error('不存在该用户');
        }
        $adminData = $adminData->toArray();
        //判断密码是否正确
        if($adminData['password'] != md5($password.$adminData['salt'])){
            return error('密码错误');
        }
        $updateData = [
            "id" => $adminData["id"],
            "last_login_time" => time(),
            "last_login_ip" => request()->ip(),
            "update_time" => time()
        ];

        $res = $adminModel->save($updateData, true);
        if(empty($res)){
            return error('登录失败');
        }
        //记录session
        session(config("session_admin"), $adminData);
        return success('登录成功');
    }

}